<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:zj="http://www.zju.edu.cn" exclude-result-prefixes="zj">
  <xsl:import href="Utility.xslt"/>
  <xsl:template name="zj:DisplayValue">
    <xsl:param name="Field"/>
    <xsl:param name="Value"/>
    <xsl:variable name="CheckValue" select="zj:Extension/@CheckValue"/>
    <xsl:choose>
      <xsl:when test="$Field/zj:HtmlCtrl/@HtmlCtrl='DetailMultiSelect'">
        <xsl:call-template name="zj:DisplayMultiSelect">
          <xsl:with-param name="Field" select="$Field"/>
          <xsl:with-param name="Value" select="$Value"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$Field/zj:CodeTable != ''">
        <!--xsl:value-of select="$zj:Toolkit/*[local-name()=$Field/zj:CodeTable][CODE_VALUE=$Value]/CODE_NAME"/-->
        <xsl:variable name="CodeName" select="$zj:Toolkit/*[local-name()=$Field/zj:CodeTable][CODE_VALUE=$Value]/CODE_NAME"/>
        <xsl:choose>
          <xsl:when test="$CodeName != ''">
            <xsl:value-of select="$CodeName"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$Value"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$Field/zj:EasySearch/@RegName != ''">
        <xsl:value-of select="$zj:Toolkit/*[local-name()=$Field/zj:FieldName][CODE_VALUE=$Value]/CODE_NAME"/>
      </xsl:when>
      <xsl:when test="$Field/zj:HtmlCtrl/@HtmlCtrl='CheckBox' and $Value=$CheckValue">
        <xsl:value-of select="'√'"/>
      </xsl:when>
      <xsl:when test="$Field/zj:HtmlCtrl/@HtmlCtrl='CheckBox' and (not ($Value) or $Value!=$CheckValue)">
        <xsl:value-of select="'×'"/>
      </xsl:when>
      <xsl:when test="$Field/@DataType = 'Date'">
        <xsl:value-of select="substring($Value, 1, 10)"/>
      </xsl:when>
      <xsl:when test="$Field/@DataType = 'DateTime'">
        <xsl:value-of select="translate(substring($Value, 1, 19), 'T', ' ')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="zj:FormatNumber">
          <xsl:with-param name="Field" select="$Field"/>
          <xsl:with-param name="Value" select="$Value"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="zj:DisplayMultiSelect">
    <xsl:param name="Field"/>
    <xsl:param name="Value"/>
    <xsl:variable name="CodeTable" select="$zj:Toolkit/*[local-name()=$Field/zj:CodeTable]"/>
    <xsl:variable name="SYhao">'</xsl:variable>
    <xsl:variable name="DisplayValue">
      <xsl:for-each select="$CodeTable">
        <xsl:if test="contains($Value,concat($SYhao, CODE_VALUE, $SYhao))">
          <xsl:value-of select="CODE_NAME"/>
          <xsl:text>, </xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="StrValue" select="string($DisplayValue)"/>
    <xsl:if test="string-length($StrValue) > 2">
      <xsl:value-of select="substring($StrValue, 1, string-length($StrValue) - 2)"/>
    </xsl:if>
  </xsl:template>
  <xsl:template name="zj:ImgPath">
    <xsl:param name="Field"/>
    <xsl:param name="DataRecord"/>
    <xsl:param name="FieldList"/>
    <xsl:param name="Src"/>
    <xsl:choose>
      <xsl:when test="$Field/zj:Upload/@Kind='File' or $Field/zj:Upload/@Kind='FileDB'">
        <xsl:value-of select="$DataRecord/*[local-name()=$Field/zj:Upload/@ServerPathField]"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="IDValue">
          <xsl:call-template name="zj:ListQueryString">
            <xsl:with-param name="DataRecord" select="$DataRecord"/>
            <xsl:with-param name="FieldList" select="$FieldList"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="concat('../Pages/WebDownloadPage.zjx?XmlFile=', ancestor::zj:Table/@XmlFile, '&amp;', $IDValue, 'DownloadField=', $Field/zj:Upload/@ContentField, '&amp;Src=', $Src)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="zj:DisplayUpload">
    <xsl:param name="Field"/>
    <xsl:param name="DataRecord"/>
    <xsl:param name="FieldList"/>
    <xsl:variable name="Link">
      <xsl:call-template name="zj:ImgPath">
        <xsl:with-param name="Field" select="$Field"/>
        <xsl:with-param name="DataRecord" select="$DataRecord"/>
        <xsl:with-param name="FieldList" select="$FieldList"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="Value" select="$DataRecord/*[local-name()=$Field/zj:FieldName]"/>
    <xsl:if test="$Value != ''">
      <a href="{$Link}" target="_blank">
        <xsl:value-of select="$Value"/>
      </a>
      <xsl:variable name="Size" select="$DataRecord/*[local-name()=$Field/zj:Upload/@SizeField] div 1024"/>
      <xsl:value-of select="concat('(', format-number($Size, '0'), 'K)')"/>
      <xsl:if test="$Field/zj:Upload/@IsView = 'true'">
        <img src="../images/lookup.gif" alt="预览" style="border=0;cursor:hand" align="middle" width="18" height="17" name="{$Field/zj:FieldName}_viewImg" onmouseover="ViewUpload('{translate($DataRecord/*[local-name()=$Field/zj:Upload/@ServerPathField], &quot;\&quot;, &quot;/&quot;)}')"/>
      </xsl:if>
    </xsl:if>
  </xsl:template>
  <xsl:template name="zj:FormatNumber">
    <xsl:param name="Field"/>
    <xsl:param name="Value"/>
    <xsl:choose>
      <xsl:when test="($Field/@DataType = 'double' or $Field/@DataType = 'int') and ($Field/zj:Display/@Format and $Value != '')">
        <xsl:variable name="Temp">
          <xsl:value-of select="format-number($Value, $Field/zj:Display/@Format)"/>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="$Temp='NaN'">
            <xsl:value-of select="$Value"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$Temp"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$Value"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="zj:HRefAddress">
    <xsl:param name="HRefType"/>
    <xsl:param name="Link"/>
    <xsl:param name="RefValue"/>
    <xsl:param name="Value"/>
    <xsl:choose>
      <xsl:when test="$HRefType='HREF'">
        <xsl:choose>
          <xsl:when test="contains($Link, '?')">
            <xsl:value-of select="concat($Link, '&amp;ID=', $RefValue, '&amp;RetURL=', $zj:SelfURL)"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="concat($Link, '?ID=', $RefValue, '&amp;RetURL=', $zj:SelfURL)"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$Value"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="zj:ListQueryString">
    <xsl:param name="DataRecord"/>
    <xsl:param name="FieldList"/>
    <xsl:variable name="IdFields" select="$FieldList/ancestor::zj:Table/@IdFields"/>
    <xsl:choose>
      <xsl:when test="$IdFields != ''">
        <xsl:value-of select="concat('ID=', $DataRecord/*[local-name()=$IdFields], '&amp;')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="KeyFields" select="$FieldList[boolean(string(@IsKey))]"/>
        <xsl:variable name="KeyCount" select="count($KeyFields)"/>
        <xsl:choose>
          <xsl:when test="$KeyCount = 1">
            <xsl:value-of select="concat('ID=', $DataRecord/*[local-name()=$KeyFields/zj:FieldName], '&amp;')"/>
          </xsl:when>
          <xsl:when test="$KeyCount > 1">
            <xsl:for-each select="$KeyFields">
              <xsl:variable name="value">
                <xsl:choose>
                  <xsl:when test="@DataType='Date'">
                    <xsl:value-of select="substring($DataRecord/*[local-name()=current()/zj:FieldName], 1, 10)"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="$DataRecord/*[local-name()=current()/zj:FieldName]"/>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
              <xsl:value-of select="concat(zj:FieldName, '=', $value, '&amp;')"/>
            </xsl:for-each>
            <xsl:value-of select="'ID=-1&amp;'"/>
          </xsl:when>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="zj:Attribute">
    <xsl:param name="Field"/>
    <xsl:for-each select="$Field/zj:Display/Attribute">
      <xsl:attribute name="{@Name}"><xsl:value-of select="@Value"/></xsl:attribute>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
